package com.xoa.plugin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.xoa.dao.attend.AttendLeaveMapper;
import com.xoa.dao.flowhook.FlowHookMapper;
import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.global.proxy.TriggerSubject;
import com.xoa.model.attend.AttendLeave;
import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.model.workflow.FlowProcess;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.util.DateFormat;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2017/8/4.
 */
@Service
public class AttendLeaveTrigger implements TriggerSubject {
    @Autowired
    private FlowHookMapper flowHookMapper;
    @Autowired
    private AttendLeaveMapper attendLeaveMapper;
    @Resource
    private FlowProcessMapper flowProcessMapper;

    @Override
    public void doRun(String company, Object... ags) {
        ContextHolder.setConsumerType(company);
        TriggerPlugIn tri = (TriggerPlugIn) ags[0];
        AttendLeave attendLeave = new AttendLeave();
        if (tri.getFlowPrcs().equals("0")) {
            attendLeave.setAllow("1");
        } else {
            attendLeave.setAllow("0");
        }

        FlowHookWithBLOBs flowHookNew = new FlowHookWithBLOBs();
        flowHookNew.setHid(tri.getHookId());
        FlowHookWithBLOBs flowHook = flowHookMapper.selectByhid(flowHookNew);
        Map<Object, Object> flowHookMaps = null;
        if (flowHook != null) {
            flowHookMaps = queryTriggerField(tri.getModify(), tri.getReject(), flowHook.getMap(), flowHook.getDataDirection());
        }

        if (flowHookMaps != null) {
            for (Map.Entry<Object, Object> entry : flowHookMaps.entrySet()) {
                if (entry.getKey().equals("LEADER_ID")) {
                    attendLeave.setLeaderId(entry.getValue().toString());
                } else if (entry.getKey().equals("LEAVE_TYPE")) {
                    attendLeave.setLeaveType(entry.getValue().toString());
                } else if (entry.getKey().equals("LEAVE_DATE1")) {
                    attendLeave.setLeaveDate1(DateFormat.getDate(entry.getValue().toString()));
                } else if (entry.getKey().equals("LEAVE_DATE2")) {
                    attendLeave.setLeaveDate2(DateFormat.getDate(entry.getValue().toString()));
                } else if (entry.getKey().equals("REASON")) {
                    attendLeave.setReason(entry.getValue().toString());
                }
            }
            attendLeave.setRunId(Integer.parseInt(tri.getRunId()));
            int a = attendLeaveMapper.updateAttendLeave(attendLeave);
        }

    }

    public Map<Object, Object> queryTriggerField(String modifydata, String fromdate, String maps, Integer num) {
        String[] a = maps.split(",");
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < a.length; i++) {
            String i1 = a[i];
            String i2[] = i1.split("=>");
            String i3 = i2[0];
            String i4 = i2[1];
            map.put(i3, i4);
        }
        JSONArray json = new JSONArray();
        Map<String, Object> from = json.parseObject(
                fromdate, new TypeReference<Map<String, Object>>() {
                });

        List<Map<String, Object>> modify = json.parseObject(modifydata, List.class);
        Map<Object, Object> analysis = new HashMap<Object, Object>();
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        for (Map<String, Object> mapmodify : modify) {
            for (Map.Entry<String, Object> entry1 : from.entrySet()) {
                if (mapmodify.get("key").equals(entry1.getValue())) {
                    analysis.put(entry1.getKey(), mapmodify.get("value"));
                }
            }
        }

        if ("1".equals(num)) {
            for (Map.Entry<Object, Object> entry : analysis.entrySet()) {//key 申请人 value  1
                for (Map.Entry<String, Object> entry1 : map.entrySet()) {
                    if (entry.getKey().equals(entry1.getValue())) {
                        returnMap.put(entry1.getKey(), entry.getValue());
                    }
                }
            }
        }
        for (Map.Entry<Object, Object> entry : analysis.entrySet()) {//key 申请人 value  1
            for (Map.Entry<String, Object> entry1 : map.entrySet()) {
                if (entry.getKey().equals(entry1.getKey())) {
                    returnMap.put(entry1.getValue(), entry.getValue());
                }
            }
        }
        return returnMap;
    }
}
